package com.hospital.diagnosis.module.utils.ai.tools;

import com.hospital.common.pojo.TAiDiagnosisRecord;
import com.hospital.common.pojo.TDepartment;
import com.hospital.diagnosis.module.dto.AiDiagnosisRecordDto;
import com.hospital.diagnosis.module.service.TAiDiagnosisRecordService;
import com.hospital.diagnosis.module.service.TDepartmentService;
import com.hospital.diagnosis.module.utils.WebSocketSessionManager;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Component
public class DiagnosisRecordTools {
    @Autowired
    private TAiDiagnosisRecordService aiDiagnosisService;
    @Autowired
    private TDepartmentService tDepartmentService;
    @Autowired
    private WebSocketSessionManager webSocketSessionManager;

    @Tool(description = "诊疗记录持久化工具")
    public String saveAiDiagnosisRecord(AiDiagnosisRecordDto diagnosisRecord) {
        TAiDiagnosisRecord aiDiagnosisRecord = new TAiDiagnosisRecord();
        BeanUtils.copyProperties(diagnosisRecord, aiDiagnosisRecord);
        aiDiagnosisRecord.setCreateTime(LocalDateTime.now());
        if(!aiDiagnosisService.save(aiDiagnosisRecord)){
            return "保存失败";
        }
        return "成功保存诊疗记录";
    }
    @Tool(description = "查询所有科室")
    public List<String> queryAllDepartment(){
        return tDepartmentService.list().stream().map(TDepartment::getName).toList();
    }
    @Tool(description = "获取当前日期")
    public String getTime() {
        // 获取当前时间,格式为yyyy-MM-dd HH:mm:ss
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return now.format(formatter);
    }
    @Tool(description = "根据对话ID获取对应的患者ID,一定要提供正确的对话ID")
    public String getPatientId(@ToolParam(description = "对话ID") String conversationId){
        return String.valueOf(webSocketSessionManager.getPatientId(conversationId));
    }
}
